<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
     <el-form-item label="名称/编码" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称/编码"></el-input>
    </el-form-item>
    <el-form-item label="所属RTU设备" prop="rtuId">
      <el-select
        class="form-item"
            v-model="dataForm.rtuId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键字"
            :remote-method="remoteMethod"
            :loading="rtuListLoading"
            v-loadmore="rtuListLoadMore">
            <el-option
            v-for="item in rtuList"
            :key="item.id"
            :label="item.serialNum+' ('+(item.farmlandName||'')+')'"
            :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="设备类型" prop="type">
       <el-select class="form-item" v-model="dataForm.type" filterable placeholder="请选设备类型">
            <el-option
            v-for="item in rtuTypeDictList"
            :key="item.value"
            :label="item.text"
            :value="item.value">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="设备图标" prop="iconImg">
      <el-input v-model="dataForm.iconImg" placeholder="设备图标"></el-input>
    </el-form-item>
    <el-form-item label="设备位置" prop="position">
      <el-input v-model="dataForm.position" placeholder="设备位置"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input-number class="form-item" v-model="dataForm.orderNum" controls-position="right" :min="0" placeholder="排序编号"></el-input-number>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-loading="formLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        formLoading: false,
        dataForm: {
          id: 0,
          rtuId: '',
          name: '',
          type: '',
          position: '',
          orderNum: '',
        },
        rtuTypeDictList:[],// RTU类型列表
        rtuListLoading: false,
        rtuList:[],// RTU列表
        rtuParams:{},// RTU查询参数
        dataRule: {
          rtuId: [
            { required: true, message: '所属rtu不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称/编码不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          // position: [
          //   { required: true, message: '设备位置不能为空', trigger: 'blur' }
          // ],
          orderNum: [
            { required: true, message: '排序编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        // 加载负责人下拉框
        this.rtuListLoadMore('init')

        // 获取农作物字典列表
        this.$http({
          url: this.$http.adornUrl('/buss/bussrtuequipment/getRtuEquipmentTypes'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.rtuTypeDictList = data.list
        }).then(() => {
            this.dataForm.id = id || 0
            this.visible = true
            this.formLoading = false
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    this.$http({
                    url: this.$http.adornUrl(`/buss/bussrtuequipment/info/${this.dataForm.id}`),
                    method: 'get',
                    params: this.$http.adornParams()
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.dataForm.rtuId = data.bussRtuEquipment.rtuId
                            this.dataForm.name = data.bussRtuEquipment.name
                            this.dataForm.type = data.bussRtuEquipment.type
                            this.dataForm.position = data.bussRtuEquipment.position
                            this.dataForm.orderNum = data.bussRtuEquipment.orderNum
                            this.dataForm.version = data.bussRtuEquipment.version
                        }
                    })
                }
            })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtuequipment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'rtuId': this.dataForm.rtuId,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'position': this.dataForm.position,
                'version': this.dataForm.version,
                'orderNum': this.dataForm.orderNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 这里下拉框查询触发的方法
      remoteMethod(query) {
        this.rtuListLoadMore('query',query)
      },
      // 这里写入滚动加载要触发的方法
      rtuListLoadMore (type,query) {
        this.rtuListLoading = true

        if(type && (type == 'init' || type == 'query')){
            this.rtuParams.page = 1
            this.rtuList = []
        }else if(this.rtuParams.page){
            if(this.rtuParams.page>=this.rtuParams.totalPage){
                this.rtuListLoading = false
                return
            }
            this.rtuParams.page += 1
        }

        var _this = this
        this.$http({
            url: this.$http.adornUrl('/buss/bussrtuinfo/list'),
            method: 'get',
            params: this.$http.adornParams({
                "serialNum": query, //搜索参数
                "page": _this.rtuParams.page || 1, //分页参数
                "limit": _this.rtuParams.limit || 15, //每次查询15条记录
            })
        }).then(({data}) => {
            _this.rtuListLoading = false
            _this.rtuParams.totalPage = data.page.totalPage
            _this.rtuList = _this.rtuList.concat(data.page.list)
        })
      }
    }
  }
</script>
